<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-10-10
 * Time: 17:22:07
 * Info:
 */

namespace plugin\huiblog\app\controller;

use support\Request;
use plugin\huiblog\app\model\ArticleCate as ArticleCateModel;
use plugin\huiblog\app\common\CacheClear;

class CateController
{

    /**
     * 分类列表
     */
    public function index(Request $request)
    {
        if ($request->isAjax()) {
            $count = (new ArticleCateModel())->count();
            $data  = ArticleCateModel::order('sort asc,id asc')->select()->toArray();

            return json(['code' => 0, 'count' => $count, 'data' => $data, 'msg' => 'ok']);
        }

        return view('admin/cate/index');
    }

    public function add(Request $request)
    {
        $id               = $request->get('id', '');
        $articleCateModel = new ArticleCateModel();
        $cates            = $articleCateModel->getPidMenuList();

        if ($request->isPost()) {
            $param = $request->post();
            if (empty($param['title'])) {
                return json(['code' => 0, 'msg' => '分类名称不能为空']);
            }
            ArticleCateModel::create($param);
            CacheClear::clearArticleCateCount();

            return json(['code' => 200, 'msg' => '操作成功']);
        }

        return view('admin/cate/add', ['id' => $id, 'cates' => $cates]);
    }

    public function edit(Request $request)
    {
        $id               = $request->get('id');
        $articleCateModel = new ArticleCateModel();
        $cates            = $articleCateModel->getPidMenuList();
        if ($request->isPost()) {
            $param = $request->post();
            if (empty($param['id'])) {
                return json(['code' => 0, 'msg' => '参数错误']);
            }
            if (empty($param['title'])) {
                return json(['code' => 0, 'msg' => '分类名称不能为空']);
            }
            ArticleCateModel::update($param, ['id' => $param['id']]);
            CacheClear::clearArticleCateCount();

            return json(['code' => 200, 'msg' => '操作成功']);
        }
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数错误']);
        }
        $data = ArticleCateModel::find($id);
        if (empty($data)) {
            return json(['code' => 0, 'msg' => '获取数据失败']);
        }

        return view('admin/cate/edit', ['data' => $data, 'cates' => $cates]);
    }

    public function delete(Request $request)
    {
        $id = $request->get('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数错误']);
        }
        ArticleCateModel::where('id', $id)->delete();
        CacheClear::clearArticleCateCount();

        return json(['code' => 200, 'msg' => '删除成功']);
    }

    /**
     * @NodeAnotation(title="更改分类显示状态")
     */
    public function status(Request $request)
    {
        if ($request->isAjax()) {
            $param = $request->all();

            $row = ArticleCateModel::find($param['id']);
            if (empty($row)) {
                return json(['code' => 0, 'msg' => '数据不存在']);
            }
            $row->save([$param['field'] => $param['val']]);

            return json(['code' => 200, 'msg' => '保存成功']);
        }
    }

}
